{extend name="../../admin/view/main"}

{block name="style"}{include file='qiyeaccident/form-style'}{/block}

{block name='content'}
<style>
    .upload-image-box {
        width: 130px;
        height: 110px;
        border: 1px solid rgba(125, 125, 125, .1);
        background-size: cover;
    }
    .upload-file-box {
        width: 80px;
        height: 50px;
        border: 1px solid rgba(125, 125, 125, .1);
        background-size: cover;
    }


</style>
<div id="infoFormEditor" class="layui-clear nowrap padding-bottom-30">
    <div class="layui-card news-right">
        <div class="layui-card-body">
            <!--<form onsubmit="return false" data-auto="true" action="{:request()->url()}" method="post" class='layui-form layui-card' autocomplete="off">-->
            <form class="layui-form padding-20" role="form" name="infoForm" onsubmit="return false">

                <label class="layui-form-item relative block">
                    <span class="color-green">事故描述</span>
                    <input maxlength="256" ng-model="item.name"  required placeholder="请在这里输入事故描述" name='name' class="layui-input">
                </label>
                <label class="layui-form-item relative block">
                    <span class="color-green">企业名称</span>
                    <input type="hidden" name="qiye_id" ng-model="item.qiye_id" value="{$qiye.id}" />
                    <input maxlength="256" value="{$qiye.name}" required placeholder="请在这里输入企业名称" name='qiye_name' class="layui-input">
                    <!--<select id="qiye_name" name="qiye_name" lay-filter="hc_select"
                            autocomplete="off" lay-search class="form-control">
                        <option value="{$qiye.id}">{$qiye.name}</option>
                    </select>-->
                </label>
                <label class="layui-form-item relative block">
                    <span class="color-green">发生时间</span>
                    <input id="occur_time" ng-model="item.occur_time" required placeholder="请在这里输入发生时间" name='occur_time' class="layui-input">
                </label>
                <label class="layui-form-item relative block">
                    <span class="color-green">认定依据</span>
                    <button style="margin-left:20px;" type="button" data-title="上传依据文件" data-file="btn"  data-field="annex_list" class="layui-btn layui-btn-sm layui-btn-primary">上传依据文件</button>
                    <div id="annex-box">
                        <!-- 这里放一些上传图片的内容 -->
                    </div>
                </label>
                <div class="layui-form-item text-center padding-top-30">
                    <input ng-model="item.annex" type="hidden" name="annex" value="" />
                    <input ng-model="item.annex_list" type="hidden" name="annex_list" value="" />
                    <input ng-model="x.read_num" type="hidden">
                    <button ng-click="submit()" type="button" class="layui-btn">保存信息</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div name="annex-template" style="display: none;">
    <div class="item" file="__URL__">
        <a target="_blank" href="__URL__">__TITLE__</a>
        <span class="font-s20 fa fa-trash"></span>
    </div>
</div>
{/block}


{block name='script'}
<script>window.form.render()</script>
<script>


    function doChoice(dataId, dataName){

        $("input[name='qiye_id']").val(dataId);
        $("input[name='qiye_name']").val(dataName);
        layer.close(layer.index);
        // alert(dataId);
        // alert(dataName);
    }




    laydate.render({
        elem: '#occur_time' //指定元素
    });



    layui.use(['form'], function () {
        var form = layui.form;
        console.log("xxxxxxxxxxxxxxxx")


        form.on('select', function (data) {   //选择移交单位 赋值给input框
            console.log("111111111111");
            console.log(data);
            console.log(data);

        });
        form.render('select');

    });




        require(['angular', 'ckeditor'], function () {

        var editor;
        var $form = $('form[name="infoForm"]');
        var $vali = $form.vali().data('validate');

        var app = angular.module("infoFormEditor", []).run(callback);
        angular.bootstrap(document.getElementById(app.name), [app.name]);

        function callback($rootScope) {
            $rootScope.list = [];
            $rootScope.item = {};
            $.form.load('{:request()->url()}', {output: 'json'}, 'get', function (ret) {
                console.log(typeof ret);
                console.log(ret);
                return $rootScope.$apply(function () {
                    apply(ret.data || {});
                }), false;
            });
                function apply(accident) {
                    if(accident.id==undefined) accident = {
                        name: '', phone: '', contacts: '',intro:'',
                        addr: '', logo: '__ROOT__/static/theme/img/image.png',annex:  '',
                    };
                    $rootScope.item = accident;
                    $('.layui-card-body.layui-hide').removeClass('layui-hide');
                    $rootScope.item.style = "background-image:url('" + $rootScope.item.logo + "')";

                    if(accident.annex) {
                        var annex = accident.annex.split(",");
                        console.log(annex);
                        for (var i = 0; i < annex.length; i++) {
                            setAnnex(annex[i]);
                        }
                    }
                }
            $rootScope.setItemValue = function (name, value) {
                $rootScope.item[name] = value;
                $rootScope.item.style = "background-image:url('" + $rootScope.item.logo + "')";
            };

            $rootScope.setItemValuel = function (name, value) {
                $rootScope.item[name] = value;
                $rootScope.item.style = "background-file:url('" + $rootScope.item.annex + "')";

            };

            $rootScope.upItem = function (index, $event) {
                $event.stopPropagation();
                var tmp = [], cur = $rootScope.list[index];
                if (index < 1) return false;
                for (var i in $rootScope.list) {
                    (parseInt(i) === parseInt(index) - 1) && tmp.push(cur);
                    (parseInt(i) !== parseInt(index)) && tmp.push($rootScope.list[i]);
                }
                apply(tmp);
            };
            $rootScope.dnItem = function (index, $event) {
                $event.stopPropagation();
                var tmp = [], cur = $rootScope.list[index];
                if (index > $rootScope.list.length - 2) return false;
                for (var i in $rootScope.list) {
                    (parseInt(i) !== parseInt(index)) && tmp.push($rootScope.list[i]);
                    (parseInt(i) === parseInt(index) + 1) && tmp.push(cur);
                }
                apply(tmp);
            };
            $rootScope.delItem = function (index, $event) {
                $event.stopPropagation();
                var list = $rootScope.list, temp = [];
                for (var i in list) (parseInt(i) !== parseInt(index)) && temp.push(list[i]);
                apply(temp);
            };
            $rootScope.submit = function () {
                $vali.checkAllInput();
                if ($form.find('.validate-error').size() > 0) {
                    return $.msg.tips('表单验证不成功，请输入需要的内容！');
                }
                var data = {
                    id: $rootScope.item.id,
                    name: $rootScope.item.name,
                    qiye_id: $("input[name='qiye_id']").val(),
                    occur_time: $("input[name=occur_time]").val(),
                    annex:$rootScope.item.annex,
                };
                $.form.load('{:request()->url()}', {data: data}, "post");
            };


            $('[name="qiye_name"]').on('focus', function (value) {
                $(this).blur();
                $.post("/cms/qiyeagent/widget",{},function(res) {
                    layer.open({
                        type: 1,area:'800px',
                        title: '选择经销商'
                        ,content: res
                    });
                },"html");
            });







            /*
                        $('[name="qiye_name"]').on('focus', function (value) {
                            $(this).blur();
                        });*/

            /*$(".layui-select-title>.layui-input").on("keyup", function () {
                var name = $(this).val();
                console.log(name);
                if(name=="" || name.length<2) return false;
                setTimeout(function() {
                    $.post("/cms/qiyeagent/ajaxSearch",{name:name},function(res){
                        console.log(res);
                        if(res.data.length>0){
                            //var html = "<option>--请选择--</option>";
                            var html = "";
                            var option = res.data;
                            for(var i=0; i<option.length; i++){
                                html += '<option value="+option[i].id+">'+option[i].name+'</option>';
                            }
                            $("select[name=hc_select]").html(html);
                            window.form.render();
                        }
                    },"json");

                },1000);*/
                    /*$.post("/cms/qiyeagent/ajaxSearch",{name:"华"},function(res){
                        console.log(res);

                        var html = "";
                        var option = res.data;
                        for(var i=0; i<option.length; i++){
                            html += '<option value="+option[i].id+">'+option[i].name+'</option>';
                        }
                        $("select[name=hc_select]").html(html);
                        window.form.render();


                    },"json");


            });*/
            $('[name="annex_list"]').on('change', function (value) {
                setAnnex(this.value);
            });
            layui.use('layer', function () {
                var $ = layui.jquery;
                $(document).on('click', '.fa-trash', function () {
                    $(this).parent().remove();
                    setAnnexList();
                });
            });
            function setAnnex(value) {
                var html = $("div[name=annex-template]").html();
                html = html.replace(/__URL__/g, value);
                html = html.replace("__TITLE__", value.substring(value.lastIndexOf("/")+1,value.length));
                $("#annex-box").append(html);
                setAnnexList();
            }
            function setAnnexList() {
                var annex = new Array();
                $("#annex-box .item").each(function () {
                    annex.push($(this).attr("file"));
                });
                $rootScope.setItemValue("annex", annex.join(","));
            }
        }

    });
</script>




{/block}